package jp.co.nes.awssupport.common.utility;

import java.util.UUID;
import java.util.concurrent.Future;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.stepfunctions.AWSStepFunctionsAsync;
import com.amazonaws.services.stepfunctions.AWSStepFunctionsAsyncClientBuilder;
import com.amazonaws.services.stepfunctions.model.StartExecutionRequest;
import com.amazonaws.services.stepfunctions.model.StartExecutionResult;

import jp.co.nes.awssupport.common.bean.BaseBean;

/**
 * AWSStepFunctionsAsyncヘルパーです。<br>
 * StepFunctionsに関する機能を提供します。
 */
public class AwsStepFunctionsAsyncHelper extends AwsHelperBase {

    /** クライアント */
    private AWSStepFunctionsAsync client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsStepFunctionsAsyncHelper() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AWSStepFunctionsAsyncClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AWSStepFunctionsAsyncClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AWSStepFunctionsAsyncClientBuilder.standard()
                            .withRegion(getRegions())
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AWSStepFunctionsAsyncClientBuilder.standard()
                            .withRegion(getRegions())
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * ステートマシンを非同期で実行します。
     *
     * @param stateMachineArn 実行するステートマシンのarn
     * @param preName 実行名のプリフィックス
     * @param input 入力内容(json形式に変換して送信します)
     * @return 実行結果が含まれるFuture
     */
    public <T extends BaseBean> Future<StartExecutionResult> startExecutionAsync(String stateMachineArn, String preName,
            T input) {
        StartExecutionRequest request = new StartExecutionRequest();
        request.setStateMachineArn(stateMachineArn);
        request.setName(preName + "-" + UUID.randomUUID().toString());
        request.setInput(JsonUtil.convertBeanToJsonString(input));
        return client.startExecutionAsync(request);
    }

    /**
     * クライアントをシャットダウンし、リソースを解放します。<br>
     * 詳細は{@link AWSStepFunctionsAsync#shutdown()}を参照してください。
     *
     * @see AWSStepFunctionsAsync#shutdown()
     */
    public void shutdown() {
        client.shutdown();
    }
}
